/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPrincipal.java
 *
 * Created on 13/12/2013, 09:55:22
 */
package views;

import config.HibernateConfig;
import controllers.ProcedimentoController;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import models.dados.HibernateUtil;

/**
 *
 * @author Wenderson
 */
public class TelaPrincipal extends javax.swing.JFrame {

    /** Creates new form TelaPrincipal */
    public TelaPrincipal() {
        initComponents();
        
        
        //<editor-fold defaultstate="collapsed" desc="Configurando Layout e Fonte do Sistema">
        try {
            UIManager.setLookAndFeel("com.alee.laf.WebLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
            this.pack();
        } catch (Exception e) {
        }
        
        this.setUIFont(new FontUIResource("Arial", Font.PLAIN, 14));
        this.setLocationRelativeTo(null);
        
        //</editor-fold>
        
        this.iniciaSessionFactory();
    }
    
    private void iniciaSessionFactory() {
        try {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    //ProcedimentoController.obterTodos();
                    HibernateUtil.restartSessionFactory();
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }                
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jm_arquivo = new javax.swing.JMenu();
        jmi_dimensao = new javax.swing.JMenuItem();
        jmi_tabAuxiliar = new javax.swing.JMenuItem();
        jmi_procedimento = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jmi_sair = new javax.swing.JMenuItem();
        jm_teste = new javax.swing.JMenu();
        jmi_teste = new javax.swing.JMenuItem();
        jm_configuracao = new javax.swing.JMenu();
        jmi_conexaoFW = new javax.swing.JMenuItem();
        jmi_conexao = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("FrameWork DW");

        jMenuBar1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        jm_arquivo.setText("Arquivo");
        jm_arquivo.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        jmi_dimensao.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_dimensao.setText("Dimensao");
        jmi_dimensao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_dimensaoActionPerformed(evt);
            }
        });
        jm_arquivo.add(jmi_dimensao);

        jmi_tabAuxiliar.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_tabAuxiliar.setText("Tab. Auxiliar");
        jmi_tabAuxiliar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_tabAuxiliarActionPerformed(evt);
            }
        });
        jm_arquivo.add(jmi_tabAuxiliar);

        jmi_procedimento.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_procedimento.setText("Procedimento");
        jmi_procedimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_procedimentoActionPerformed(evt);
            }
        });
        jm_arquivo.add(jmi_procedimento);
        jm_arquivo.add(jSeparator1);

        jmi_sair.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_sair.setText("Sair");
        jmi_sair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_sairActionPerformed(evt);
            }
        });
        jm_arquivo.add(jmi_sair);

        jMenuBar1.add(jm_arquivo);

        jm_teste.setText("Teste");
        jm_teste.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        jmi_teste.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_teste.setText("Executar Testes");
        jmi_teste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_testeActionPerformed(evt);
            }
        });
        jm_teste.add(jmi_teste);

        jMenuBar1.add(jm_teste);

        jm_configuracao.setText("Configuração");
        jm_configuracao.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N

        jmi_conexaoFW.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_conexaoFW.setText("Testar Conexão do FrameWork");
        jmi_conexaoFW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_conexaoFWActionPerformed(evt);
            }
        });
        jm_configuracao.add(jmi_conexaoFW);

        jmi_conexao.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        jmi_conexao.setText("Conexão do Banco de Teste");
        jmi_conexao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmi_conexaoActionPerformed(evt);
            }
        });
        jm_configuracao.add(jmi_conexao);

        jMenuBar1.add(jm_configuracao);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmi_dimensaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_dimensaoActionPerformed
        new TelaDimensao(this, true).setVisible(true);
    }//GEN-LAST:event_jmi_dimensaoActionPerformed

    private void jmi_sairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_sairActionPerformed
        this.dispose();
    }//GEN-LAST:event_jmi_sairActionPerformed

    private void jmi_conexaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_conexaoActionPerformed
        new TelaConexao(this, true).setVisible(true);
    }//GEN-LAST:event_jmi_conexaoActionPerformed

    private void jmi_testeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_testeActionPerformed
        new TelaExecutarTestes(this, true).setVisible(true);
    }//GEN-LAST:event_jmi_testeActionPerformed

    private void jmi_tabAuxiliarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_tabAuxiliarActionPerformed
        new TelaTabAuxiliar(this, true).setVisible(true);
    }//GEN-LAST:event_jmi_tabAuxiliarActionPerformed

    private void jmi_procedimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_procedimentoActionPerformed
        new TelaProcedimento(this, true).setVisible(true);
    }//GEN-LAST:event_jmi_procedimentoActionPerformed

    private void jmi_conexaoFWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmi_conexaoFWActionPerformed
        HibernateConfig hc = new HibernateConfig();
                
        if (hc.getProperties() == null) {
            JOptionPane.showMessageDialog(null, "Conexão do FrameWork inválida.\n\nVerifique o arquivo \"configFrameWork.xml\"");
        } else {
            JOptionPane.showMessageDialog(null, "Conexão válida.");
            HibernateUtil.restartSessionFactory();
        }
    }//GEN-LAST:event_jmi_conexaoFWActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TelaPrincipal tela = new TelaPrincipal();
                tela.setExtendedState(JFrame.MAXIMIZED_BOTH);
                tela.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenu jm_arquivo;
    private javax.swing.JMenu jm_configuracao;
    private javax.swing.JMenu jm_teste;
    private javax.swing.JMenuItem jmi_conexao;
    private javax.swing.JMenuItem jmi_conexaoFW;
    private javax.swing.JMenuItem jmi_dimensao;
    private javax.swing.JMenuItem jmi_procedimento;
    private javax.swing.JMenuItem jmi_sair;
    private javax.swing.JMenuItem jmi_tabAuxiliar;
    private javax.swing.JMenuItem jmi_teste;
    // End of variables declaration//GEN-END:variables

    private void setUIFont(FontUIResource f) {
        
        //
        // sets the default font for all Swing components.
        // ex.
        // setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
        //
        java.util.Enumeration keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof javax.swing.plaf.FontUIResource)
        UIManager.put(key, f);
        }

    }
}
